package com.android.shop.common;

import java.io.File;
import java.io.FileNotFoundException;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.ImageView;

/**
 * 异步加载图片
 */
public class AsyncImageLoad extends AsyncTask<String, Integer, Uri> {
	private ImageView imageView;
	private static File file;
	private Context context;

	public AsyncImageLoad(Context context, ImageView imageView, String path) {

		if (file == null) {
			file = new File(Environment.getExternalStorageDirectory(),"OrderShop");	
		}
		if (!file.exists())
			file.mkdirs();
		this.imageView = imageView;
		this.context = context;
		this.execute(path);
	}

	protected Uri doInBackground(String... params) {// 子线程中执行的
		try {
			return ContactService.getImage(params[0], file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	protected void onPostExecute(Uri result) {// 运行在主线程
		if (result != null && imageView != null)
			try {
				Bitmap bitmap = BitmapFactory.decodeStream(context
						.getContentResolver().openInputStream(result));
				imageView.setImageBitmap(bitmap);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

	}
}
